package com.gem.eolane.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gem.eolane.entity.News;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Auther: cz
 * @Date: 2019/4/10 9:19
 * @Description: 新闻接口
 */
@Repository
public interface NewsMapper extends BaseMapper<News> {

    //查询最后一条新闻记录(最新上传的新闻)
    @Select("select*from news order by id DESC limit 1")
    News findLasterNews();

    //返回上一级
    @Select("update news set del_ident = 2 where id = #{preld}")
    void backUp(@Param("preld") int preld);

    //返回上一级时回显数据
    @Select("select * from news where id = #{subld} ")
    News findNewBysubld(@Param("subld") Integer subld);

    //
    @Select("UPDATE news SET title=#{news.title}, author=#{news.author}, category=#{news.category}, language=#{news.language}, " +
            "release_time=#{news.releaseTime}, image_address=#{news.imageAddress}, content_address=#{news.contentAddress} WHERE id=#{news.id}")
    void updateNewsBysubld(@Param("news") News news);

    //确认提交
    @Select("update news set del_ident = 0 where id = #{preld}")
    void confirmSubmit(@Param("preld") int preld);

    //删除新闻
    @Select("update news set del_ident = 1 where id = #{id}")
    void deleteNewsById(@Param("id") int id);

    //从后查询4条记录行业新闻
    @Select("select * from news where category = 1 and del_ident = 0 and language = 0 order by release_time desc limit 4;")
    List<News> findIndustryNewsDesc() ;
    //查询最新的公司新闻
    @Select("select * from news where category = 0 and del_ident = 0 and language = 0 order by release_time desc limit 1;")
    News findNewCompanyNews();
    //从后查询4条记录公司新闻
    @Select("select * from news where category = 0 and del_ident = 0 and language = 0 order by release_time desc  limit 4;")
    List<News> findCompanyNewsDesc();

    //分页查询公司新闻
    //select * from news  where category = 1 order by id desc limit 0 , 6 desc
    @Select("select * from news  where category = 0 and del_ident = 0 and language = 0 order by release_time desc limit #{currIndex} , #{pageSize}")
    List<News> findPageCompanyNews(@Param("currIndex") int currIndex, @Param("pageSize") int pageSize);


    //分页查询行业新闻
    //select * from news  where category = 1 order by id desc limit 0 , 6 desc ,and del_ident = 0
    @Select("select * from news  where category = 1 and del_ident = 0 and language = 0  order by release_time desc limit #{currIndex} , #{pageSize}")
    List<News> findPageIndustryNews(@Param("currIndex") int currIndex, @Param("pageSize") int pageSize);
    //查询公司新闻总记录数
    //select count(*) from players where tname  like '%${value}%'
    @Select("select count(*) from news where category = 0 and del_ident = 0 and language = 0 ")
    int getCompanyTotal();
    //查询行业新闻总记录数
    //select count(*) from players where tname  like '%${value}%'
    @Select("select count(*) from news where category = 1 and del_ident = 0 and language = 0 ")
    int getIndustryTotal();

    //根据id查找新闻内容
    @Select("select * from news where id = #{id} ")
    News findNewsContent(@Param("id") int id);

}
